package main

import "fmt"

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

type ListNode struct {
	Val  int
	Next *ListNode
}

func createLine(valList []int) *ListNode {
	head := &ListNode{}
	cur := head
	for _, val := range valList {
		cur.Next = &ListNode{Val: val}
		cur = cur.Next
	}
	return head.Next
}

func printLine(head *ListNode) {
	for head != nil {
		fmt.Print(head.Val)
		head = head.Next
	}
	fmt.Println()
}

func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
	head := &ListNode{}
	pos := head
	for list1 != nil && list2 != nil {
		if list1.Val <= list2.Val {
			pos.Next = list1
			list1 = list1.Next
		} else {
			pos.Next = list2
			list2 = list2.Next
		}
		pos = pos.Next
	}
	if list1 != nil {
		pos.Next = list1
	} else {
		pos.Next = list2
	}
	return head.Next
}
func main() {
	printLine(mergeTwoLists(createLine([]int{1, 2, 4}), createLine([]int{1, 3, 4})))
}
